﻿using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;
using eduscrum.DAL;
using eduscrum.Helper;
using eduscrum.Models;
using eduscrum.ViewModels;
using System;

namespace eduscrum.Controllers
{ 
    public class SprintController : Controller
    {
        private SystemContext db = new SystemContext();

        public ViewResult IndexProject(int projectID)
        {
            var sprints = db.Sprints.Where(s => s.Project.ProjectID == projectID).ToList();
            List<SprintVM> sprintsVM = AutoMapper.Mapper.Map<List<Sprint>, List<SprintVM>>(sprints);
            return View(sprintsVM);
        }

        public ViewResult Index(int projectID)
        {
            var sprints = db.Sprints.Where(s => s.Project.ProjectID == projectID).ToList();
            List<SprintVM> sprintsVM = AutoMapper.Mapper.Map<List<Sprint>, List<SprintVM>>(sprints);
            return View(sprintsVM);
        }

        //
        // GET: /Sprints/Create

        public ActionResult Create()
        {
            var viewModel = new SprintVM();
            viewModel.StartDate = DateTime.Now;
            viewModel.EndDate = DateTime.Now;
            return View("Create", viewModel);
        } 

        //
        // POST: /Sprints/Create

        [HttpPost]
        public ActionResult Create(SprintVM sprintToCreate, string[] sprintbackloglistcreate)
        {
            int projectID = -1;
            if (Request["ProjectID"] != null)
            {
                projectID = Int32.Parse(Request["ProjectID"].ToString());
                Project project = db.Projects.Find(projectID);
                ProjectVM projectVM = new ProjectVM();
                AutoMapper.Mapper.Map(project, projectVM);
                sprintToCreate.Project = projectVM;
            }
            sprintToCreate.SprintBacklog = new List<UserStoryVM>();
            Project projectToAddSprintTo = db.Projects.Find(projectID);
            if (ModelState.IsValid)
            {
                Sprint newSprint = new Sprint();
                AutoMapper.Mapper.Map(sprintToCreate, newSprint);

                if (sprintbackloglistcreate != null)
                {
                    foreach (string userStoryID in sprintbackloglistcreate)
                    {
                        UserStory userStory = db.UserStories.Find(Int32.Parse(userStoryID));
                        userStory.Sprint = newSprint;
                        db.Entry(userStory).State = EntityState.Modified;
                    }
                }
                newSprint.Project = projectToAddSprintTo;
                projectToAddSprintTo.Sprints.Add(newSprint);
                db.Entry(projectToAddSprintTo).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Edit", new RouteValueDictionary(new { controller = "Project", action = "Edit", id = projectID }));
            }
            return View(sprintToCreate);
        }
        
        //
        // GET: /Sprints/Edit/5
 
        public ActionResult Edit(int id)
        {
            Sprint sprintToEdit = db.Sprints.Find(id);
            SprintVM viewModel = new SprintVM();
            AutoMapper.Mapper.Map(sprintToEdit, viewModel);
            return View("Edit", viewModel);
        }

        //
        // POST: /Sprints/Edit/5

        [HttpPost, ActionName("Edit")]
        public ActionResult Edit(SprintVM sprintToEditVM, string[] sprintbackloglistedit)
        {
            int projectID = Int32.Parse(Request["ProjectID"].ToString());
            sprintToEditVM.Project = new ProjectVM { ProjectID = projectID };
            
            Sprint sprintEdited = db.Sprints.Find(sprintToEditVM.SprintID);
            Project project = sprintEdited.Project;
            AutoMapper.Mapper.Map(sprintToEditVM, sprintEdited);
            sprintEdited.Project = project;
            if (ModelState.IsValid)
            {
                db.UserStories.Where(u => u.Sprint.SprintID == sprintEdited.SprintID).ToList().ForEach(u => u.Sprint = null);
                if (sprintbackloglistedit != null)
                {
                    foreach (string userStoryID in sprintbackloglistedit)
                    {
                        UserStory userStory = db.UserStories.Find(Int32.Parse(userStoryID));
                        userStory.Sprint = sprintEdited;
                        db.Entry(userStory).State = EntityState.Modified;
                    }
                }
                db.Entry(sprintEdited).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Edit", new RouteValueDictionary(new { controller = "Project", action = "Edit", id = project.ProjectID }));
            }
            return View(sprintToEditVM);
            
        }

        //
        // GET: /Sprints/Delete/5
 
        public ActionResult Delete(int id)
        {
            Sprint sprintToDelete = db.Sprints.Find(id);
            Project project = sprintToDelete.Project;
            db.UserStories.Where(u => u.Sprint.SprintID == id).ToList().ForEach(u => u.Sprint = null);
            db.Sprints.Remove(sprintToDelete);
            db.Entry(project).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Edit", new RouteValueDictionary(new { controller = "Project", action = "Edit", id = project.ProjectID }));
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}